package com.tff.gmall.manger.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tff.gmall.beans.PmsProductImage;
import com.tff.gmall.beans.PmsProductInfo;
import com.tff.gmall.manger.upload.ImgeUploadUtil;
import com.tff.gmall.service.SpuService;
import org.csource.common.MyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@CrossOrigin
public class SpuController {

    @Reference
    private SpuService spuService;

    //spuList?catalog3Id=61
    @RequestMapping(value = "/spuList")
    @ResponseBody
    public List<PmsProductInfo> spuList(String catalog3Id) {

        return spuService.spuListService(catalog3Id);
    }

    /***
     * 商品SKU保存接口
     * @param pmsProductInfo
     * @return
     */
    @RequestMapping(value = "/saveSpuInfo")
    @ResponseBody
    public Boolean saveSpuInfo(@RequestBody PmsProductInfo pmsProductInfo) {
        boolean falg = spuService.saveSpuInfo(pmsProductInfo);
        return falg;
    }

    /***
     * 根据商品id查询该商品的所有图片
     * @return
     */
    @RequestMapping(value = "/spuImageList")
    @ResponseBody
    public List<PmsProductImage> getSpuImageList(String spuId) {
        return spuService.spuImageListService(spuId);
    }


    /***
     * 图片上传接口
     * @param multipartFile
     * @return
     */
    @RequestMapping(value = "/fileUpload")
    @ResponseBody
    public String fileUpload(@RequestParam("file") MultipartFile multipartFile) {
        String url = "";
        //具体做上传的事情
        try {
            url = ImgeUploadUtil.uploadImg(multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        System.out.println(url);
        return url;
    }

}
